import { Component, OnInit, Injector } from '@angular/core';
import { AddBase } from 'app/modules/base/add-base';
import { PersonnelService } from '../../services/personnel.service';
import { markAllAsTouched, formGroupToFormRawData } from 'app/modules/base/helper';

@Component({
  selector: 'm-add',
  templateUrl: './add.component.html',
  styleUrls: ['./add.component.scss'],
})
export class AddComponent extends AddBase implements OnInit {
  constructor(private injector: Injector, private personnelService: PersonnelService) {
    super(injector);
  }
  formData = this.personnelService.formData;
  personnelForm = this.personnelService.formGroup;

  ngOnInit() { }

  async submit() {
    markAllAsTouched(this.personnelForm);
    if (this.personnelForm.invalid) {
      return;
    }
    const data = formGroupToFormRawData(this.personnelForm);
    await this.personnelService.create(data);
    this.back();
  }

  switchGenPsw() {
    const passwordSwitchControl = this.personnelForm.controls['random_password_switch'];
    if (passwordSwitchControl.value) {
      const result = this.makepassword();
      this.personnelForm.get('ldap_password').disable({emitEvent: true});
      this.personnelForm.get('ldap_password_confirm').disable({emitEvent: true});
      this.personnelForm.patchValue({
        ldap_password: result,
        ldap_password_confirm: result
      })
    } else {
      this.personnelForm.get('ldap_password').enable();
      this.personnelForm.get('ldap_password_confirm').enable();
      this.personnelForm.patchValue({
        ldap_password: '',
        ldap_password_confirm: ''
      })
    }
  }

  makepassword_(length) {
    var result = '';
    var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(),.';
    var charactersLength = characters.length;
    for (var i = 0; i < length; i++) {
      result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
  }

  getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

  shuffleArray(array) {
    for (var i = array.length - 1; i > 0; i--) {
      var j = Math.floor(Math.random() * (i + 1));
      var temp = array[i];
      array[i] = array[j];
      array[j] = temp;
    }
    return array;
  }

  passwordLength = 8;
  addUpper = true;
  addNumbers = true;
  addSymbols = true;

  makepassword() {

    const lowerCharacters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
    const upperCharacters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    const numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    // const symbols = ['-', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '[', '\\', ']', '{', '}', ',', '.', '<', '>', '+', '?', ':', '\'', '"', '|', '/', '~', '=', ';', '`'];
    let noOfLowerCharacters = 0,
      noOfUpperCharacters = 0,
      noOfNumbers = 0,
      noOfSymbols = 0;

    const noOfneededTypes = Number(this.addUpper) + Number(this.addNumbers) + Number(this.addSymbols);

    noOfLowerCharacters = this.getRandomInt(1, this.passwordLength - noOfneededTypes);

    let usedTypeCounter = 1;

    if (this.addUpper) {
      noOfUpperCharacters = this.getRandomInt(1, this.passwordLength - noOfneededTypes + usedTypeCounter - noOfLowerCharacters);
      usedTypeCounter++;
    }

    if (this.addSymbols) {
      noOfSymbols = 1;
      usedTypeCounter++;
    }

    if (this.addNumbers) {
      noOfNumbers = this.passwordLength - noOfneededTypes + usedTypeCounter - noOfLowerCharacters - noOfUpperCharacters - noOfSymbols;
    }

    var passwordArray = [];

    for (var i = 0; i < noOfLowerCharacters; i++) {
      passwordArray.push(lowerCharacters[this.getRandomInt(1, lowerCharacters.length - 1)]);
    }

    for (var i = 0; i < noOfUpperCharacters; i++) {
      passwordArray.push(upperCharacters[this.getRandomInt(1, upperCharacters.length - 1)]);
    }

    for (var i = 0; i < noOfNumbers; i++) {
      passwordArray.push(numbers[this.getRandomInt(1, numbers.length - 1)]);
    }

    for (var i = 0; i < noOfSymbols; i++) {
      passwordArray.push('#');
    }
    
    passwordArray = this.shuffleArray(passwordArray);

    return passwordArray.join("");
  };
}

const TestData = {
  utype: '1',
  full_name: 'test',
  job_number: '123456789',
  ldap_username: 'test',
  ldap_password: 'test',
  password_verify: 'test',
  department_id: '',
  position_id: '',
  sex: '',
  birth_year: '',
  birth_month: '',
  phone: '17339602197',
  email: 'wurongjie1@gmail.com',
  photo: '',
};
